<?php


namespace app\service\impl;


use app\model\MqttUserManage;
use app\service\PanelService;
use fast\Http;
use think\facade\Cache;
use think\facade\Log;

class PanelHIKComServiceImpl implements PanelService{
    var $_config;

    public function __construct(){
        $this->_config=config('capture.panel_com');
    }

    public function add_group($group_name,$ape_id,$third_code){
        return $third_code;
    }

    public function add_people($id, $name, $card_num, $photo, $right){
        $mqtt_user=MqttUserManage::find($id);
        $data['gender']=1;
        $data['personName']=$name;
        $data['credentialType']=3;
        $data['credentialNumber']=$mqtt_user['phone'];
        $data['mobile']=$mqtt_user['phone'];
        $data['faceUrl']=str_replace(config('capture.apply_path'),$this->_config['photo_url'],$mqtt_user['photo']);
        //Log::info('图片：'.$photo);
        Log::info('新增人员req:'.json_encode($data));
        //Log::info('url'.$this->_config['url'].'basic/persons/create');
        $res=Http::post($this->_config['url'].'estate/system/person',json_encode($data),$this->getToken());
        Log::info('新增人员resp:'.$res);
        $res=json_decode($res,true);
        if($res&&$res['code']==200){
            sleep(5);
            $person_id=$res['data']['personId'];
            //批量获取设备列表
            $device['communityId']=$right;
            $device['pageNo']=1;
            $device['pageSize']=100;
            Log::info('设备列表req:'.json_encode($device));
            $res1=Http::post($this->_config['url'].'estate/devices/actions/listByCommunityId',json_encode($device),$this->getToken());
            Log::info('设备列表resp1:'.$res1);
            $res1=json_decode($res1,true);
            if($res1&&$res1['code']==200){
                $lists=$res1['data']['rows'];
                foreach ($lists as $list){
                    $device_ids[]=$list['deviceId'];
                }
                //批量下发
                $op['operationType']=0;
                $op['communityId']=$right;
                $op['personIds']=[$person_id];
                $op['deviceIds']=$device_ids;
                Log::info('批量下发req:'.json_encode($op));
                sleep(5);
                $res2=$card_res=Http::post($this->_config['url'].'estate/entranceGuard/permissions/actions/batchAuthorityIssued',json_encode($op),$this->getToken());
                Log::info('批量下发resp:'.$card_res);
                return $res2;
            }else{
                return $res1;
            }
        }else{
            return $res;
        }
    }

    private function getToken() {
        $token=Cache::get("Hik_com_token");
        if(!$token){
            $data['client_id']=$this->_config['client_id'];
            $data['client_secret']=$this->_config['client_sec'];
            $data['grant_type']='client_credentials';
            //$data['client_id']=$this->_config['client_id'];
            Log::info('token req:'.json_encode($data));
            $res=Http::post('https://api2.hik-cloud.com/oauth/token',$data);
            Log::info('token resp:'.$res);
            if($res){
                $res=json_decode($res,true);
                $token=$res['token_type'].' '.$res['access_token'];
                Cache::set('Hik_com_token',$token,$res['expires_in']);
            }
        }
        return [CURLOPT_HTTPHEADER=>['Content-Type:application/json','Authorization:'.$token]];
    }

    public function bind_group($op_rights, $ape_id) {
        $data['groupId']=$op_rights;
        $data['checkCapability']=true;
        $data['deviceSerials']=[$ape_id];
        $data['autoIssue']=true;
        Log::info('绑定权限req:'.json_encode($data));
        $res=Http::post($this->_config['url'].'accessControl/permissionGroups/actions/addDevices',json_encode($data),$this->getToken());
        Log::info('绑定权限resp:'.$res);
    }
}